<?php
namespace Clover\Controller;

use Think\Controller;

class AuthorController extends BaseController
{
    public function __construct()
    {
        parent::__construct();
        session('menu', "author");
    }

    /**
     * 产品员工列表
     * @param int cat_id 默认值1
     */
    public function index()
    {
        check_privilege();
        session('menu_sub', "author_list");
        $cat_id = I("get.cat_id", 0);

        $category_list = M("book_author")->table("dsk_book_author_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $author_list = D("Author")->get_author_list($cat_id);
        $data = array(
            "category_list" => $result,
            "author_list" => $author_list,
            "cat_id" => $cat_id
        );
        $this->assign($data);
        $this->display("author_list");
    }

    /**
     * 跳转到添加页面
     * @return [type] [description]
     */
    public function add_author()
    {
        check_privilege();
        $author_id = I("get.id", 0);
        $category_list = M("book_author")->table("dsk_book_author_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "author_id" => $news_id,
            "category_list" => $result,
        );
        $this->assign($data);
        $this->display("author_add");
    }

    /**
     * 执行员工添加操作
     */
    public function do_add()
    {
        $author_name = I("post.author_name", "");
        $cat_id = I("post.cat_id", 0);
        $author_intro = I("post.author_intro", "", false);
        $sort_order = I("post.sort_order", 0, false);
        $author_img = I("post.author_img", "");
        $Author = M("Author");
        $data = array(
            "author_name" => $author_name,
            "cat_id" => $cat_id,
            "author_intro" => $author_intro,
            "sort_order" => $sort_order,
            "author_img" => $author_img,
            "add_time" => time()
        );
        $status = $Author->table("dsk_book_author")->data($data)->add();
        if ($status) {
            echo json_encode(array("status" => $status));
        } else {
            echo json_encode(array("status" => -1));
        }
    }

    /**
     * 跳转到修改页面
     * @return [type] [description]
     */
    public function edit_author()
    {
        check_privilege();
        $author_id = I("get.id", 0);
        $category_list = M("book_author_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $author_info = M("book_author")->where("id=" . $author_id)->find();
        $data = array(
            "author_id" => $author_id,
            "category_list" => $result,
            "author_info" => $author_info,
            "add_time" => time()
        );
        $this->assign($data);
        $this->display("author_edit");
    }

    /**
     * 执行新闻修改操作
     */
    public function do_edit()
    {
        $id = I("post.id");
        $author_name = I("post.author_name", "");
        $cat_id = I("post.cat_id", 0);
        $author_intro = I("post.author_intro", "", false);
        $sort_order = I("post.sort_order", 0, false);
        $author_img = I("post.author_img", "");
        $Author = M("Author");
        $data = array(
            "author_name" => $author_name,
            "cat_id" => $cat_id,
            "author_intro" => $author_intro,
            "sort_order" => $sort_order,
            "author_img" => $author_img,
            "add_time" => time()
        );
        $res = $Author->table("dsk_book_author")->where("id=$id")->save($data);
        echo $res;
    }

    /**
     * 执行新闻删除
     * @return [type] [description]
     */
    public function do_delete()
    {
        $id = I("post.id", 0);
        $Author = M("Book_author");
        $res = $Author->table("dsk_book_author")->where("id=$id")->delete();
        echo $res;
    }

    /**
     * 员工分类列表页面
     *
     */
    public function category_list()
    {
        check_privilege();
        session('menu_sub', "author_cat");
        $category_list = M("book_author_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "category_list" => $result
        );
        $this->assign($data);
        $this->display("category_list");
    }

    /**
     * 跳转到分类信息添加页面
     */
    public function add_category()
    {
        check_privilege();
        $Author = M("Author");
        $category_list = $Author->table("dsk_book_author_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level");
        $data = array(
            "category_list" => $result
        );
        $this->assign($data);
        $this->display("category_add");
    }

    public function do_add_category()
    {
        $parent_id = I("post.parent_id", 0);
        $cat_name = I("post.cat_name", "");
        $sort_order = I("post.sort_order", 0);
        $Author = D("Author");
        $data = array(
            "parent_id" => $parent_id,
            "cat_name" => $cat_name,
            "sort_order" => $sort_order,
            "add_time" => time()
        );
        $status = $Author->table("dsk_book_author_category")->data($data)->add();
        echo $status;
    }

    /**
     * 跳转到分类信息修改页面
     */
    public function edit_category()
    {
        check_privilege();
        $id = I("get.id", 0);
        $Author = D("Author");
        $category_list = $Author->table("dsk_book_author_category")->order(array('sort_order' => "desc", 'id' => "asc"))->select();
        infiniteAssort($category_list, 0, 0, "parent_id", "id", $result, "p_level", 1, array('id' => $id));
        $category_info = $Author->table("dsk_book_author_category")->where("id=" . $id)->find();
        $data = array(
            "category_list" => $result,
            "category_info" => $category_info,
            "cat_id" => $id
        );
        $this->assign($data);
        $this->display("category_edit");
    }

    public function do_edit_category()
    {
        $cat_name = I("post.cat_name", "");
        $sort_order = I("post.sort_order", 0);
        $id = I("post.id", 0);

        $Author = D("Author");
        $data = array(
            "cat_name" => $cat_name,
            "sort_order" => $sort_order
        );
        $status = $Author->table("dsk_book_author_category")->where("id=" . $id)->save($data);
        if ($status) {
            echo $status;
        } else {
            echo -1;
        }
    }

    /**
     * 执行新闻分类删除同时删除旗下子集
     * @return [type] [description]
     */
    public function do_delete_category()
    {
        $cat_id = I("post.id", 0);
        $del_ids = $cat_id;
        $childrenIds = D("Author")->get_Children_Ids($cat_id);
        if ($childrenIds) {
            $del_ids .= $childrenIds;
        }
        $Author = M("Author");
        $map['id'] = array('in', $del_ids);
        $status = $Author->table("dsk_book_author_category")->where($map)->delete();
        echo $status;
    }

    //上传cover
    public function upload_cover()
    {
        $upload = new \Think\UploadFile();// 实例化上传类
        $upload->maxSize = 5242880;// 5M设置附件上传大小
        $upload->allowExts = array('jpg', 'gif', 'png', 'jpeg');// 设置附件上传类型
        $savepath = './Public/Uploads/' . date('Y-m-d') . '/';
        if (!file_exists($savepath)) {
            mkdir($savepath);
        }
        $upload->savePath = $savepath;// 设置附件上传目录
        if (!$upload->upload()) {// 上传错误提示错误信息
            echo json_encode(array('code' => 0, 'message' => '上传失败'));
        } else {// 上传成功 获取上传文件信息
            $info = $upload->getUploadFileInfo();
            $file_name = date('Y-m-d') . "/" . $info[0]['savename'];
            echo json_encode(array('code' => 1, 'file_name' => $file_name));
        }
    }

    /**
     * 执行banner删除操作
     */
    public function do_delete_pic()
    {
        $id = I("post.id", 0);
        $book_info = M("Books")->where("id=$id")->find();
        $res = 1;
        if (!empty($book_info)) {
            $src = __ROOT__ . 'Public/Uploads/' . $book_info['banner_img'];
            if (file_exists($src)) {
                unlink($src);
                $res = M("Books")->where("id=$id")->delete();
            }
        }
        echo $res;
    }
}